package com.sfac.springCloudTest.controller;


import com.github.pagehelper.PageInfo;
import com.sfac.springCloudTest.service.CityService;
import org.example.common.vo.Result;
import org.example.common.vo.Search;
import org.example.test.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author jzq
 * @date 2022/8/18 9:44
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/api/test")
public class CityController {

    @Autowired
    private CityService cityService;


    /**
     * /api/city  --- post
     * {"cityName":"test","localCityName":"测试"}
     *
     * @param city
     * @return
     */
    @PostMapping(value = "/city", consumes = "application/json")
    Result<City> insertCity(@RequestBody City city) {
        return cityService.insertCity(city);
    }


    /**
     * /api/city  --- put
     * {"cityId":"2259","cityName":"liyue","localCityName":"璃月"}
     *
     * @param city
     * @return
     */
    @PutMapping(value = "/city", consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city) {
        return cityService.updateCity(city);
    }


    /**
     * /api/city/2259  --- get
     *
     * @param cityId
     * @return
     */
    @GetMapping("/city/{cityId}")
    public City getCityByCityId(@PathVariable int cityId) {
        return cityService.getCityByCityId(cityId);
    }


    /**
     * /api/city/?cityName=dream11&localCityName=梦想之城11   --- get
     *
     * @param cityName
     * @param localCityName
     * @return
     */
    @GetMapping("/city")
    public City getCityByNames(@RequestParam String cityName,
                               @RequestParam String localCityName) {
        return cityService.getCityByNames(cityName, localCityName);
    }


    /**
     * /api/cities/522    --- get
     *
     * @param countryId
     * @return
     */
    @GetMapping("/cities/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable int countryId) {
        return cityService.getCitiesByCountryId(countryId);
    }


    /**
     * /api/city/2259  --- delete
     *
     * @param cityId
     * @return
     */
    @DeleteMapping("/city/{cityId}")
    public Result<Object> deleteCityByCityId(@PathVariable int cityId) {
        return cityService.deleteCityByCityId(cityId);
    }


    /**
     * /api/cities     ----post
     * {"currentPage":"1","pageSize":"5","sort":"city_name","direction":"desc","keyword":""}
     *
     * @param search
     * @return
     */
    @PostMapping(value = "/cities", consumes = "application/json")
    public PageInfo<City> getCitiesBySearch(@RequestBody Search search) {
        return cityService.getCitiesBySearch(search);
    }

}
